<?php
/**
 * utf 字符串截取
 * @param  [sring] $str
 * @param  [int] $from
 * @param  [int] $len
 * @return [string]
 */
function utf8Substr($str, $from, $len)
{
	return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$str);
} 
/**
 * 生成等比例縮略圖
 * @param  [string] $src
 * @param  string $alt
 * @param  string $width
 * @param  string $height
 * @param  string $parameters
 * @return [string]
 */
function tep_image($src, $alt = '', $width = '', $height = '', $parameters = '') {
    $image = '<img src="' . tep_output_string($src) . '" alt=""';
    if (tep_not_null($alt)) {
      $image .= ' title=""';
    }
	if (file_exists($alt))
	{
		list($imgWidth, $imgHeight, $type, $attr) = getimagesize($alt);	
		if ( empty($height) || $height==0  )
		{
			$width_fix  = $width;
			$height_fix = $width * $imgHeight/$imgWidth;
		}
		elseif ( empty($width) || $width==0 )
		{
			$width_fix  = $height * $imgWidth/$imgHeight;
			$height_fix = $height;	
		}
		else
		{
			if ($imgWidth <= $width && $imgHeight <= $height)
			{
				$width_fix  = $imgWidth;
				$height_fix = $imgHeight;
			}
			else
			{
				$width_fix  = $width;
				$height_fix = $width * $imgHeight/$imgWidth;
				if ($height_fix > $height)
				{
					$width_fix  = $height * $imgWidth/$imgHeight;
					$height_fix = $height;
				}
			}
		}		  
	  }
    

    if (tep_not_null($width) && tep_not_null($height)) {
      $image .= ' width="' . tep_output_string($width_fix) . '" height="' . tep_output_string($height_fix) . '" border="0"';
    }

    if (tep_not_null($parameters)) $image .= ' ' . $parameters;

    $image .= ' />';

    return $image;
 }
function tep_output_string($string, $translate = false, $protected = false) {
    
      return $string;
   
  }
 /**
  * 獲取產品圖片的第一張圖片
  * @param  [int] $id
  * @return [string]
  */
function get_first_image($id){
	$image = M('photo_images')->where(array('news_id' => $id))->order('sort_order desc')->limit(1)->getField('image');	
	return $image ;
}
function cate_list($parent_id = 1)
{
	$cate = M()->query('select c.cate_id,cd.cate_name from cms_cate c join cms_cate_description cd on c.cate_id = cd.cate_id
									where cd.language_id = '.L('lan_id').' and  c.status = 1 and c.parent_id = '.$parent_id.'
									order by c.sort_order desc , cd.cate_name asc');
	return $cate;
}
function research_list()
{
	$research_cate = M()->query('select c.cate_id,cd.cate_name from cms_program_cate c join cms_program_cate_description cd on c.cate_id = cd.cate_id
								where cd.language_id = '.L('lan_id').' and  c.status = 1
								order by c.sort_order desc , cd.cate_name asc');
	return $research_cate;
}
?>